package com.zj.core.adapter

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.dialogutils.ToastUtil
import com.fileutils.SPUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.zj.core.R
import com.zj.core.base.BaseApplication
import com.zj.core.bean.TemplateTitleBean
import com.zj.core.bean.VoiceTemplateBean
import com.zj.core.net.NetPresenter
import com.zj.core.net.NetValueCallBackIml
import com.zj.core.ui.ListFormItemUi
import com.zj.core.ui.TemplateListItemUi
import com.zj.core.ui.TemplateListUi
import org.jetbrains.anko.image

import java.util.ArrayList

class AdapterParent(
    private val listParen: ArrayList<TemplateTitleBean.Rows>?,
    private val context: Context,
    private val mHospitalId: String,
    private val mDepatmentId: String
) : RecyclerView.Adapter<AdapterParent.ParentHolder>() {
    private var mGson: Gson?= Gson()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParentHolder {
        return ParentHolder(TemplateListItemUi(context).record_item_parent_view!!)
    }

    override fun onBindViewHolder(holder: ParentHolder, position: Int) {
        if (holder is ParentHolder) {
            val viewIsShow = booleanArrayOf(false)

            with(holder){
                this.title.text = listParen?.get(position)?.meaning
                this.rootView.setOnClickListener{
                    if (viewIsShow[0]){
                        viewIsShow[0] = false
                        this.recyclerViewChi.visibility = View.GONE
                        this.tv_show.image=context.resources.getDrawable(R.mipmap.record_down_img)
                    } else {
                        viewIsShow[0] = true
                        this.recyclerViewChi.visibility = View.VISIBLE
                        this.tv_show.image=context.resources.getDrawable(R.mipmap.record__up_img)
                        requestTemplateDetail(listParen?.get(position)?.meaning.toString(),this,position)
                    }
                }
            }
        }
    }

    /**
     * 请求模板详情
     */
    private fun requestTemplateDetail(templateTitle:String, holder: ParentHolder, position: Int){
        NetPresenter().getVoiceTemplate(
            object: NetValueCallBackIml {
                override fun onComplete() {}
                override fun onSubscribe(data: Any?) {}
                override fun onNext(values: Any?, bean: Any?) {
                    if ((values as ArrayList<VoiceTemplateBean.VoiceWordBean.DatEntity.RowsEntity>).size != 0) {
                        val adapterChilder = AdapterChilder(context, values, position)
                        holder.recyclerViewChi.layoutManager = LinearLayoutManager(context)
                        holder.recyclerViewChi.adapter = adapterChilder
                    } else {
                        ToastUtil.showShort(BaseApplication.context, "未获取到$bean")
                    }
                }
                override fun onError() {
                    ToastUtil.showShort(BaseApplication.context, "模板数据获取失败")
                }
            },
            templateTitle,
            mHospitalId,
            mDepatmentId
        )
    }

    override fun getItemCount(): Int {
        return listParen?.size!!
    }

    fun getListViewItem():String{
        var mResultStr=""
        for(childItem in 0 until listParen?.size!!){
            val mObj= SPUtils.get(BaseApplication.context,""+childItem,"")
            if(mObj!=null&&mObj!=""){
                val map=mGson?.fromJson(mObj as String,HashMap<String,String>().javaClass)
                for (entry in map?.entries!!) {
                    mResultStr=mResultStr+entry.value+"\n"
                }
            }
            SPUtils.put(BaseApplication.context,""+childItem,"")
        }
        return mResultStr
    }

    class ParentHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var title: TextView=itemView.findViewById(R.id.tv_parent_name)
        var recyclerViewChi: RecyclerView = itemView.findViewById(R.id.list_chil)
        var tv_show: ImageView= itemView.findViewById(R.id.tv_show)
        var rootView: LinearLayout= itemView.findViewById(R.id.lv_parent_item)
    }
}